package cn.e2win.biz.base;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import cn.e2win.biz.util.CloneUtils;
import cn.e2win.biz.util.DateUtils;
import cn.e2win.biz.util.StringUtils;

public class QueryHelper {

	//忽略
	private static final String _I = "_I";
	//<>
	private static final String _N = "_N";
	//LIKE
	private static final String _L = "_L";
	//NOT LIKE
	private static final String _NL = "_NL";
	//DATE_BEGIN >=
	private static final String _B = "_B";
	//DATE_BEGIN >
	private static final String _NE = "_NE";
	//DATE_END
	private static final String _E = "_E";
	//DATE_END <
	private static final String _NB = "_NB";
	//IS NULL
	private static final String _NULL = "_NULL";
	//IS NOT NULL
	private static final String _NNULL = "_NNULL";
	

	
	
	
	private String sql;
	private Map<String, Object> dbMap;
	public QueryHelper(String originalSql, Map<String, ?> originalMap) {
		super();
		this.dbMap = CloneUtils.clone(originalMap);
		this.sql = processSql(originalSql,originalMap);
		System.err.println(sql);
		System.err.println(dbMap);
	}
	
	
	
	
	

	private String processSql(String sql, Map<String, ?> scMap){
		String returnSql = "";
		if(scMap!=null){
			for(Iterator<?> it=scMap.entrySet().iterator();it.hasNext();){
				Entry<String, Object> entry = (Entry<String, Object>) it.next();
				if(entry.getValue()!=null && !StringUtils.isEmpty(entry.getValue().toString())){
					returnSql = returnSql + getOperator(entry.getKey());
				}
			}
		}
		if(!"".equals(returnSql)){
			sql = sql.replaceAll("1=1", "1=1 " +returnSql);
		}
		return sql;
	}
	private String getOperator (String field){
		
			String key = field;
			String oper = " = :";
			if(field.endsWith(_L)){
				key = field.substring(0, field.length() - _L.length());
				oper = " LIKE :";
				dbMap.put(field, "%" + dbMap.get(field) + "%");
			}else if(field.endsWith(_B)){
				key = field.substring(0, field.length() - _B.length());
				oper = " >= :";
				if(field.endsWith("Date" + _B)){
					dbMap.put(field, DateUtils.str2Date((String)dbMap.get(field)));
				}else if(field.endsWith("id" + _B) || field.endsWith("Id" + _B) || field.endsWith("Int" + _B)){
					dbMap.put(field, Integer.parseInt((String)dbMap.get(field)));				
				}else if(field.endsWith("Float" + _B)){
					dbMap.put(field, Float.parseFloat((String)dbMap.get(field)));				
				}else{
					dbMap.put(field, dbMap.get(field));	
				}	
			}else if(field.endsWith(_NE)){
				key = field.substring(0, field.length() - _NE.length());
				oper = " > :";
				if(field.endsWith("Date" + _NE)){
					dbMap.put(field, DateUtils.str2Date((String)dbMap.get(field)));
				}else if(field.endsWith("id" + _NE) || field.endsWith("Id" + _NE) || field.endsWith("Int" + _NE)){
					dbMap.put(field, Integer.parseInt((String)dbMap.get(field)));				
				}else if(field.endsWith("Float" + _NE)){
					dbMap.put(field, Float.parseFloat((String)dbMap.get(field)));				
				}else{
					dbMap.put(field, dbMap.get(field));	
				}	
			}else if(field.endsWith(_NB)){
				key = field.substring(0, field.length() - _NB.length());
				oper = " < :";
				if(field.endsWith("Date" + _NB)){
					dbMap.put(field, DateUtils.getOffsetDay(DateUtils.str2Date((String)dbMap.get(field)), 1));
				}else if(field.endsWith("id" + _NB) || field.endsWith("Id" + _NB) || field.endsWith("Int" + _NB)){
					dbMap.put(field, Integer.parseInt((String)dbMap.get(field)));				
				}else if(field.endsWith("Float" + _NB)){
					dbMap.put(field, Float.parseFloat((String)dbMap.get(field)));				
				}else{
					dbMap.put(field, dbMap.get(field));	
				}		
			}else if(field.endsWith(_E)){
				key = field.substring(0, field.length() - _E.length());
				oper = " <= :";
				if(field.endsWith("Date" + _E)){
					dbMap.put(field, DateUtils.getOffsetDay(DateUtils.str2Date((String)dbMap.get(field)), 1));
				}else if(field.endsWith("id" + _E) || field.endsWith("Id" + _E) || field.endsWith("Int" + _E)){
					dbMap.put(field, Integer.parseInt((String)dbMap.get(field)));				
				}else if(field.endsWith("Float" + _E)){
					dbMap.put(field, Float.parseFloat((String)dbMap.get(field)));				
				}else{
					dbMap.put(field, dbMap.get(field));	
				}	
			}else if(field.endsWith(_N)){
				key = field.substring(0, field.length() - _N.length());
				oper = " <> :";
//以下代码会将field 设为 空字符串，所以必须放在最后。
			} else if(field.endsWith(_NULL)){
				key = field.substring(0, field.length() - _NULL.length());
				oper = " IS NULL ";
				field = "";
			} else if(field.endsWith(_NNULL)){
				key = field.substring(0, field.length() - _NNULL.length());
				oper = " IS NOT NULL ";
				field = "";
			} 
		if(field.endsWith(_I)){
			return "";
		}else{
			return " AND " + key + oper + field;
		}
	}
	
	
	
	
	
	public String getSql() {
		return sql;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
	public Map<String, Object> getDbMap() {
		return dbMap;
	}
	public void setDbMap(Map<String, Object> dbMap) {
		this.dbMap = dbMap;
	}
}
